package com.ht.module.info.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.constant.BusConstant;
import com.ht.constant.DicConstants;
import com.ht.module.bus.entity.BusFile;
import com.ht.module.bus.service.IBusFileService;
import com.ht.module.common.service.CommonService;
import com.ht.module.info.entity.InfoMerchants;
import com.ht.module.info.mapper.InfoMerchantsMapper;
import com.ht.module.info.service.IInfoMerchantsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.module.sys.entity.SysRole;
import com.ht.module.sys.entity.SysUser;
import com.ht.module.sys.entity.SysUserRole;
import com.ht.module.sys.service.ISysRoleService;
import com.ht.module.sys.service.ISysUserRoleService;
import com.ht.module.sys.service.ISysUserService;
import com.ht.util.CommMethod;
import com.ht.util.Pager;
import com.ht.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商户表 服务实现类
 * </p>
 *
 * @author Auto-generator
 * @since 2021-12-03
 */
@Service
public class InfoMerchantsServiceImpl extends ServiceImpl<InfoMerchantsMapper, InfoMerchants> implements IInfoMerchantsService {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private IBusFileService busFileService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Override
    @Transactional
    public void merchantsIn(InfoMerchants infoMerchants) {
        SysUser sysUser = new SysUser()
                .setUsername(infoMerchants.getUsername())
                .setState(DicConstants.State.DISABLE)
                .setName(infoMerchants.getName())
                .setPassword(infoMerchants.getPassword());
        //新增用户
        sysUserService.saveObj(sysUser);
        //设置用户角色
        SysRole sysRole = sysRoleService.getRoleByCode(DicConstants.RoleCode.MERCHANTS);
        //保存角色
        sysUserRoleService.save(new SysUserRole().setRoleId(sysRole.getId()).setUserId(sysUser.getId()));
        //设置营业执照图片地址
        infoMerchants.setBusinessLicensePath(commonService.uploadFile(infoMerchants.getLicenseFile()));
        //保存商户信息
        infoMerchants.setUserId(sysUser.getId());
        //保存商户信息
        this.save(infoMerchants);
        //上传商户图片
        for (MultipartFile multipartFile : infoMerchants.getImgList()) {
            //上传商户图片
            BusFile busFile=new BusFile();
            CommMethod.beanCreate(busFile);
            busFile.setFileBusiness(DicConstants.FileBusiness.MERCHANTS)
                    .setPath(commonService.uploadFile(multipartFile))
                    .setName(multipartFile.getOriginalFilename())
                    .setFileType(CommMethod.getFileSuffix(Objects.requireNonNull(multipartFile.getOriginalFilename())))
                    .setSize(new BigDecimal((multipartFile.getSize()/1024)+"").setScale(2, BigDecimal.ROUND_HALF_UP))
                    .setTableId(infoMerchants.getId());
            busFileService.save(busFile);
        }
    }

    @Override
    public Pager<InfoMerchants> findPage(Pager<InfoMerchants> pager, InfoMerchants en) {
        QueryWrapper<InfoMerchants> qw=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(en.getState())){
            qw.eq("tab.state",en.getState());
        }
        if(StrUtil.isNotEmpty(en.getUsername())){
            qw.like("tab.username",en.getUsername());
        }
        if(StrUtil.isNotEmpty(en.getName())){
            qw.like("tab.name",en.getName());
        }
        if(StrUtil.isNotEmpty(en.getContact())){
            qw.like("tab.contact",en.getContact());
        }
        return baseMapper.findPage(pager,qw);
    }

    @Override
    @Transactional
    public void deleteById(String id) {
        //查询当前商户
        InfoMerchants merchants = this.getById(id);
        this.removeById(id);
        sysUserService.removeById(merchants.getUserId());
    }

    @Override
    public InfoMerchants findByMe() {
        InfoMerchants infoMerchants = this.getOne(new QueryWrapper<InfoMerchants>().eq("user_id", UserUtil.getUserId()));
        return infoMerchants;
    }
}
